package org.sepheriel.airy2d.event;

import java.util.ArrayList;

public class EventManager {
	
	private ArrayList<EventReceiver> _receivers;
	private ArrayList<Event> _events;
	
	public EventManager()
	{
		_receivers = new ArrayList<EventReceiver>();
		_events = new ArrayList<Event>();
	}
	
	public void subscribe(EventReceiver _eventReceiver)
	{
		_receivers.add(_eventReceiver);
	}
	
	public void queueEvent(Event _event)
	{
		_events.add(_event);
	}
	
	public void triggerEvent(Event _event)
	{
		for(EventReceiver er: _receivers)
		{
			er.onEvent(_event);
		}
		if(_events.contains(_event))
		{
			_events.remove(_event);
		}
	}
	
	public void execute()
	{
		for(Event e: _events)
		{
			for(EventReceiver er: _receivers)
			{
				er.onEvent(e);
			}
		}
		_events.clear();
	}

}
